//
//  ListViewController.m
//  JZMCFM
//
//  Created by Mc on 15/10/30.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "ListViewController.h"
#import "ListViewCell.h"
#import "listHelper.h"
#import "focusImagesModel.h"
#import "LiveDetailController.h"
#import "programAndhostModel.h"
#import "ListDetail1ViewController.h"

#define kScreenwidth [UIScreen mainScreen].bounds.size.width
@interface ListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [super viewDidLoad];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    
    [self.mainTableView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [[listHelper sharedInstance]downloadDataFromNetWork:^{
        
        [self.mainTableView reloadData];
    }];

    
}


#pragma 分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
#pragma mark cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        
        //加判断,防止崩溃
        if ([listHelper sharedInstance].programArray.count != 0) {
            
            programAndhostModel *model = [listHelper sharedInstance].programArray[indexPath.row];
            [cell setCellDataWithpragram:model];
            
            
        }
    }else{
        if ([listHelper sharedInstance].hostArray.count != 0) {
            programAndhostModel *model = [listHelper sharedInstance].hostArray[indexPath.row];
            [cell setCellDataWithpragram:model];
        }
        
    }
    
    
    return cell;
    
}

#pragma mark cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [ListViewCell height];
    
}

#pragma mark 分组头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 180;
    }
    
    return 25;
    
}

#pragma mark 自定义section的头部
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenwidth, 180)];
        view.backgroundColor = [UIColor whiteColor];
        
        NSInteger count = [listHelper sharedInstance].focusImageArray.count;
        
        //循环数组
        for (NSInteger i = 0; i< count; i++) {
            focusImagesModel *model = [[focusImagesModel alloc]init];
            model = [listHelper sharedInstance].focusImageArray[i];
            UIButton *button = [[UIButton alloc]init];
            button.frame = CGRectMake(0, 0,kScreenwidth, 155);
            
            NSURL *url = [NSURL URLWithString:model.pic];
            NSData *data =[NSData dataWithContentsOfURL:url];
            UIImage *image =[UIImage imageWithData:data];
            
            [button setBackgroundImage:image forState:UIControlStateNormal];
            button.backgroundColor = [UIColor yellowColor];
            
            //为button添加点击时间事件
            [button addTarget:self action:@selector(buttonDIdClick) forControlEvents:UIControlEventTouchDown];
            
            [view addSubview:button];
            
        }
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 155, 25, 25)];
        [image setImage:[UIImage imageNamed:@"iconfont-weibiaoti103-2"]];
        [view addSubview:image];
        
        UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(25, 155, 350, 25)];
        lable1.text = @"节目榜单";
        lable1.font = [UIFont systemFontOfSize:13];
        [view addSubview:lable1];
        
        return view;
    }else{
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenwidth, 25)];
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
        [image setImage:[UIImage imageNamed:@"iconfont-weibiaoti103-2"]];
        [view addSubview:image];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(25,0,350, 25)];
        label.text = @"主播榜单";
        label.font = [UIFont systemFontOfSize:13];
        
        [view addSubview:label];
        return view;
    }
    
}


#pragma mark cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


    
    //判断分区,传值
    if (indexPath.section == 0) {
        LiveDetailController *listVC = [[LiveDetailController alloc]init];
        //属性传值(把网址的key值传过去)
        programAndhostModel *model = [listHelper sharedInstance].programArray[indexPath.row];
        NSString *string = model.key;
        NSString *string1 = model.contentType;
        listVC.receive = string;
        listVC.receive2 = string1;
        
        [self.navigationController pushViewController:listVC animated:YES];
    }else{
        
        ListDetail1ViewController  *listVC = [[ListDetail1ViewController alloc]init];
        programAndhostModel *model = [listHelper sharedInstance].hostArray[indexPath.row];
        NSString *string = model.key;
        NSString *string1 = model.contentType;
        listVC.receive = string;
        listVC.receive1 = string1;
        [self.navigationController pushViewController:listVC animated:YES];
    }
}



#pragma mark button的点击事件
#warning 暂时没有实现了还
-(void)buttonDIdClick{
    
    
    
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
